from core.tasks.TaskEngine import  *
from core.spider.SpiderTaskContxet import  *
from core.spider.DriveImpl import  *
from core.spider.easy.EasyTaskBuidder import  *
from core.spider.easy.EasyTaskContext import  *
#爬虫引擎
class SpiderTaskEngine(TaskEngine):

    spiderTaskContxet = None
    task = None
    driveImpl = None

    # 创建事件对象
    ctsToken = threading.Event()

    #构造函数
    def __init__(self,task):
        self.task = task
        self.spiderTaskContxet = SpiderTaskContxet(self)

    #停止
    def stop(self):
        self.ctsToken.clear()

    def handler(self):

        easyTaskContext = EasyTaskContext(self.spiderTaskContxet)
        easyTaskContext.crawlUrlSwitch = True
        easyTaskContext.postSwitch = True
        easyTaskContext.replySwitch = True

        easyTaskBuidder  = EasyTaskBuidder(easyTaskContext)
        workers = easyTaskBuidder.handler()

        #转发
        self.driveImpl = DriveImpl(self.ctsToken)
        self.driveImpl.adds(workers)
        self.driveImpl.execute()